<?php

namespace app\controller;
use app\model\ReaderModel;
use think\annotation\route;
use think\db\exception\DbException;
use think\facade\Db;
use think\response\Json;

class ReaderController
{
    /**
     * 批量插入读者，并初始化密码为 [123456]
     * @route ("insertReaders")
     */
    public function insertReaders($readers, ReaderModel $readerModel): int
    {
        return $readerModel->insertReaders($readers);
    }

    /**
     * @route ("deleteReaders")
     * @param $ids
     * @param ReaderModel $readerModel
     * @return Json
     */
    public function deleteReaders($ids, ReaderModel $readerModel): Json
    {
        //        echo Db::getLastSql()."\n";
        return json($readerModel->deleteReaders($ids));
    }

    /**
     * @route ("getThisReaderById")
     * @param $id
     * @param ReaderModel $readerModel
     * @return ReaderModel
     */
    public function getThisReaderById($id, ReaderModel $readerModel): ReaderModel
    {
        return $readerModel->getThisReaderById($id);
    }

    /**
     * @route ("loginRequest")
     * @param $userName
     * @param $password
     * @param ReaderModel $readerModel
     */
    public function loginRequest($userName, $password, ReaderModel $readerModel)
    {
        return json($readerModel->getThisReaderById($userName, $password));
    }

    /**
     * 由于update只能返回更新后对象的目标数据，所以处理后直接返回true，若报500在返回异常
     * @route ("updateReaders")
     * @param $readers
     * @param ReaderModel $readerModel
     * @return Json
     */
    public function updateReaders($readers, ReaderModel  $readerModel): Json
    {
        $readerModel->updateReaders($readers);
        return json(["result"=>true]);
    }

}